public class CountBits {

    public int[] countBits(int n) {
        int[] ans = new int[n + 1];
        for (int i = 1; i <= n; i++) {
            int temp = i;
            for (int j = 0; j < 32; j++) {
                ans[i] += temp & 1;
                temp >>= 1;
            }
        }
        return ans;
    }
}
